#include <bits/stdc++.h>
using namespace std;

map<string,vector<string>> mp;
vector<string> city;

int main()
{
  int n;  cin>>n;
 
  for(int i=1;i<=n;++i)
  {
    string a,b;cin>>a>>b;
    if(!mp.count(b))
    {
      city.push_back(b);
    }    
    mp[b].push_back(a);
 
  }
  // for(int i=0;i<city.size();++i)
  // {
  //   cout<<city[i]<<" "<<mp[city[i]].size()<<endl;
  //   for(int j=0;j<mp[city[i]].size();++j)
  //   {
  //     cout<<mp[city[i]][j]<<endl;
  //   }
  // }
  for(auto &a:city)
  {
    cout<<a<<" "<<mp[a].size()<<endl;
    for(auto&j:mp[a])
    {
      cout<<j<<endl;
    }
  }
  return 0;
}
